package com.example.waimai.contraller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.waimai.common.R;
import com.example.waimai.pojo.Employee;
import com.example.waimai.service.ServiceEmployee;
import io.swagger.annotations.Api;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;


@Slf4j
@RestController
@RequestMapping("/employee")
@Api(tags = "套餐相关接口")
public class EmployeeContraller {
    @Autowired
    private ServiceEmployee serviceEmployee;


    /*登录*/
    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee){
        //密码加密
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());

        //在数据库中查询对比
        LambdaQueryWrapper<Employee> lqw=new LambdaQueryWrapper<>();
        lqw.eq(Employee::getUsername, employee.getUsername());
        Employee emp = serviceEmployee.getOne(lqw);

        //比对用户
        if(emp==null){
            return R.error("登录失败");}
        //比对密码
        if(!emp.getPassword().equals(password)){
            return R.error("登录失败");}

        //状态为0，返回
        if(emp.getStatus()==0){
            return R.error("账号被禁用");
        }

        //
        request.getSession().setAttribute("employee",emp.getId());

        return R.success(emp);
    }

    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request){
        request.getSession().getAttribute("employee");//清空Session中保存员工的当前id
        return R.success("退出成功");
    }

    @PostMapping//添加员工
    public R<String> add(HttpServletRequest request,@RequestBody Employee employee){
        log.info("");
        //设置初始值
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        /*employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());*/

        //得到创建人id
        //long userId = (long)request.getSession().getAttribute("employee");
        /*employee.setCreateUser(userId);
        employee.setUpdateUser(userId);*/

        serviceEmployee.save(employee);
        return R.success("新增员工成功");
    }

    @GetMapping("/page")
    public R<Page> getPage(int page,int pageSize,String name){
        log.info("page:{},pageSize:{},name:{}",page,pageSize,name);
        //构造分页构造器
        Page pageinfo=new Page(page,pageSize);

        //构造查询构造器
        LambdaQueryWrapper<Employee> lwq=new LambdaQueryWrapper<>();
        lwq.like(StringUtils.isNotEmpty(name),Employee::getName,name);//判断name是否为null 并查找

        //添加排序条件
        lwq.orderByAsc(Employee::getCreateTime);
        //执行程序
        serviceEmployee.page(pageinfo, lwq);
        return R.success(pageinfo);
    }

    //禁用 启用员工账号
    @PutMapping
    public R<String> ban(HttpServletRequest request,@RequestBody Employee employee){
        //得到员工id
        long id = Thread.currentThread().getId();
        log.info("线程id为：{}",id);
        /*long employee1 = (long)request.getSession().getAttribute("employee");
       employee.setUpdateTime(LocalDateTime.now());//更新时间
        employee.setUpdateUser(employee1);//更新更新人*/
        serviceEmployee.updateById(employee);
        return R.success("员工更新成功");
    }

    //编辑员工信息
    @GetMapping("/{id}")
    public R<Employee> UpdateById(@PathVariable Long id) {
        Employee byId = serviceEmployee.getById(id);
        if (byId != null) {
            return R.success(byId);
        }else{
            return R.error("没有查询到，数据错误");
        }
    }

}
